package com.solcoo.customer.ui.reg;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.android.common.App;
import com.android.common.util.DateUtil;
import com.android.common.util.DialogUtil;
import com.android.common.util.ToastUtil;
import com.solcoo.customer.Constant;
import com.solcoo.customer.R;
import com.solcoo.customer.base.BaseActivity;
import com.solcoo.customer.base.task.TaskHelper;
import com.solcoo.customer.base.task.TaskHelper.Callback;
import com.solcoo.customer.model.CheckCode;
import com.solcoo.customer.util.CheckUtil;
import com.solcoo.customer.util.CheckUtil.CheckResult;

public class CheckCodeClickListener implements OnClickListener {
	private CheckCodeCallBack callback;
	private BaseActivity mActivity;
	private Dialog mDialog;
	private CheckCode mResult;
	private PhoneNoLoader mPhoneNoLoader;
	private Dialog errorDialog;
	private int codeTime = 0;
	private CodeTask mCodeTask = new CodeTask();
	private Button btn;
	private String text;

	public CheckCodeClickListener(PhoneNoLoader phoneNoLoader,
			BaseActivity activity, CheckCodeCallBack callback, Button btn) {
		mActivity = activity;
		this.mPhoneNoLoader = phoneNoLoader;
		this.callback = callback;
		this.btn = btn;
		text = btn.getText().toString();
	}

	@Override
	public void onClick(View v) {
		final String phoneNo = mPhoneNoLoader.getPhoneNo();
		CheckResult result = CheckUtil.checkPhoneNo(phoneNo);
		if (result.isSuccess()) {
			mActivity.setProgressDialogMessage("正在请求验证码...");
			TaskHelper.oper(v.getContext(), Constant.URL.getCheckCode(phoneNo),
					getCodeCallBack, CheckCode.class);
		} else {
			ToastUtil.showToast(v.getContext(), result.failMsg);
		}
	}

	public void startDisabled() {
		btn.setEnabled(false);
		codeTime = 60;
		mCodeTask.millisecond = (int) DateUtil.ONE_SECONDS;
		mHandler.postDelayed(mCodeTask, 0);
	}

	private Callback<CheckCode> getCodeCallBack = new Callback<CheckCode>() {
		@Override
		public void onSuccess(CheckCode result) {
			mResult = result;
			if (mDialog == null) {
				mDialog = DialogUtil.createDialog(mActivity,
						successOnClickListener, null, null,
						App.getString(R.string.checkcode_dialog_message),
						App.getString(R.string.checkcode_dialog_title));
			}
			startDisabled();
			mDialog.show();
		}

		@Override
		public void onFail(CheckCode result) {
			btn.setEnabled(true);
			if (errorDialog == null) {
				errorDialog = DialogUtil.createDialog(mActivity, null, null,
						null, result.failMsg, null);
			}
			errorDialog.show();
		}
	};

	public interface CheckCodeCallBack {
		void onGetCode(String code);
	}

	public interface PhoneNoLoader {
		String getPhoneNo();
	}

	private android.content.DialogInterface.OnClickListener successOnClickListener = new android.content.DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			callback.onGetCode(mResult.msg);
		}
	};

	private class CodeTask implements Runnable {
		public int millisecond;

		@Override
		public void run() {
			mHandler.sendEmptyMessage(millisecond);
		}
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (--codeTime > 0) {
				btn.setText(text + "(" + codeTime + ")");
				this.postDelayed(mCodeTask, msg.what);
			} else {
				btn.setText(text);
				btn.setEnabled(true);
			}
		}
	};
}
